package com.enzoism.springboot.dive.bis.datastruct;

/**
 * 模拟堆数据
 * 特点：先进后出
 * 有三个核心方法
 * 1）pop  返回第一个元素，不出栈
 * 2）peek 返回第一个元素，并出栈
 * 3）push 放入对象
 */
public class MyStack {
    private MyNode top;

    public MyStack(MyNode top) {
        this.top = top;
    }

    public static void main(String[] args) {
        MyStack data1 = new MyStack(new MyNode("data1"));
        System.out.println("------>dada：" + data1);

        data1.push(new MyNode("data2"));
        System.out.println("------>dada：" + data1);

        MyNode pop = data1.pop();
        System.out.println("------>pop：" + pop);
        System.out.println("------>dada：" + data1);

        MyNode peek = data1.peek();
        System.out.println("------>peek：" + peek);
        System.out.println("------>dada：" + data1);

    }

    @Override
    public String toString() {
        return "MyStack{" +
                "top=" + top +
                '}';
    }

    public MyNode pop() {
        if (null == top) {
            return null;
        }
        return new MyNode(top.data);
    }

    public MyNode peek() {
        if (null == top) {
            return null;
        }
        MyNode tempTop = new MyNode(top.data);
        top = top.next;
        return tempTop;
    }

    public void push(MyNode node) {
        if (null == top) {
            top = node;
        } else {
            node.next = top;
            top = node;
        }
    }
}
